﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;
public partial class sysadmin_DL_Project_ManageClassTask : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string CourseNum = string.Empty;
    private string CourseID = string.Empty;
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    VOTOClassTasks voClassTask = new VOTOClassTasks();
    TOClassTasks objClassTask = new TOClassTasks();
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    //TOUserOfClass objClassUser = new TOUserOfClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        InitPage();
        int CID = 0;
        if (Request.QueryString["CID"] != null)
        {
            CID = int.Parse(Request.QueryString["CID"].ToString());
            ViewState["CID"] = CID.ToString();
            voClass = objClass.Load(CID);
            ClassName = voClass.Name;
            BinderPG(voClass);
        }
    } 

    protected override void InitPage()
    {
        //chkAll.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        //chkCancel.Text = GetLanageValueByKey("chkbox_ManagerQuestion_chkboxCancel", PowerCreator.LMS.Language.LanagetItemType.Page);

        //btn_DelCheck.Text = GetLanageValueByKey("btnDel", PowerCreator.LMS.Language.LanagetItemType.Page);
        //btnAddNew.Text = GetLanageValueByKey("btn_AddNew", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        base.InitPage();

        //btnAddNew.Visible = false;
        //if (IsLicensed(PowerCreator.LMS.Action.Core.ActionInfo.AddJob))
        //    btnAddNew.Visible = true;

    }



    private void BinderPG(VOTOClass vo)
    {
        VOTOCourseList voCourselist = new VOTOCourseList();
        TOCourseList objCourselist = new TOCourseList();
        voCourselist = objCourselist.Load(vo.ClassCourseListID);
        string CourseIDsList = voCourselist.CourseID;
        string[] courseid = CourseIDsList.Split(',');
        CourseNum = courseid.Length.ToString();
        string courseidlist = string.Empty;
        for (int i = 0; i < courseid.Length; i++)
        {
            string[] par = courseid[i].Split('#');
            if (par.Length == 2)
            {
                if (courseidlist.Length > 0)
                    courseidlist += ",";
                courseidlist += par[0];
            }
        }
        if (courseidlist.Length == 0)
            courseidlist = "0";



        string search = "SELECT TO_Course.Course_Id, TO_Course.CourseName, TO_ClassTasks.ClassTasks_Id, TO_ClassTasks.ClassID, TO_ClassTasks.TasksTitle, TO_ClassTasks.EndDate, TO_ClassTasks.Broswer,TO_ClassTasks.CreatDate, TO_ClassTasks.SetTop, TO_ClassTasks.SetHighLight FROM TO_ClassTasks,TO_Course where TO_ClassTasks.Course_ID = TO_Course.Course_Id  and TO_ClassTasks.ClassID=" + ViewState["CID"].ToString();
        DataTable dt = obj.Query(search);
        dt.Columns.Add("strBroswer");
        dt.Columns.Add("strSetTop");
        dt.Columns.Add("strSetHighLight");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i]["Broswer"].ToString() == "True")
                dt.Rows[i]["strBroswer"] = "是";
            else
                dt.Rows[i]["strBroswer"] = "否";
            if (dt.Rows[i]["SetTop"].ToString() == "True")
                dt.Rows[i]["strSetTop"] = "是";
            else
                dt.Rows[i]["strSetTop"] = "否";
            if (dt.Rows[i]["SetHighLight"].ToString() == "True")
                dt.Rows[i]["strSetHighLight"] = "是";
            else
                dt.Rows[i]["strSetHighLight"] = "否";
        }


        search = "SELECT Course_Id, CourseName FROM TO_Course where Course_Id in (" + courseidlist + ") and Course_Id not in (select Course_ID from TO_ClassTasks where ClassID=" + ViewState["CID"].ToString() + ")";

        DataTable dt01 = obj.Query(search);


        dt01.Columns.Add("ClassTasks_Id");
        dt01.Columns.Add("ClassID");
        dt01.Columns.Add("TasksTitle");
        dt01.Columns.Add("EndDate");
        dt01.Columns.Add("Broswer");
        dt01.Columns.Add("CreatDate");
        dt01.Columns.Add("SetTop");
        dt01.Columns.Add("SetHighLight");
        dt01.Columns.Add("strBroswer");
        dt01.Columns.Add("strSetTop");
        dt01.Columns.Add("strSetHighLight");


        for (int j = 0; j < dt01.Rows.Count; j++)
        {
            dt01.Rows[j]["ClassTasks_Id"] = "0";
            dt01.Rows[j]["ClassID"] = int.Parse(Request.QueryString["CID"].ToString());
            dt01.Rows[j]["TasksTitle"] = "--";
            dt01.Rows[j]["EndDate"] = DateTime.Now;
            dt01.Rows[j]["Broswer"] = false;
            dt01.Rows[j]["SetTop"] = false;
            dt01.Rows[j]["SetHighLight"] = false;
            dt01.Rows[j]["strBroswer"] = "--";
            dt01.Rows[j]["strSetTop"] = "--";
            dt01.Rows[j]["strSetHighLight"] = "--";

            dt.Rows.Add(dt01.Rows[j]["Course_Id"], dt01.Rows[j]["CourseName"], dt01.Rows[j]["ClassTasks_Id"], dt01.Rows[j]["ClassID"], dt01.Rows[j]["TasksTitle"], dt01.Rows[j]["EndDate"], dt01.Rows[j]["Broswer"], dt01.Rows[j]["CreatDate"], dt01.Rows[j]["SetTop"], dt01.Rows[j]["SetHighLight"], dt01.Rows[j]["rowstat"], dt01.Rows[j]["strBroswer"], dt01.Rows[j]["strSetTop"], dt01.Rows[j]["strSetHighLight"]);

            
        }

        search = "SELECT  Course_ID,ClassTasks_Id, ClassID, TasksTitle, EndDate, Broswer,CreatDate, SetTop,SetHighLight FROM TO_ClassTasks where Course_ID = 0  and ClassID=" + ViewState["CID"].ToString();
        DataTable dt03 = obj.Query(search);
        dt03.Columns.Add("CourseName");
        dt03.Columns.Add("strBroswer");
        dt03.Columns.Add("strSetTop");
        dt03.Columns.Add("strSetHighLight");


        for (int k = 0; k < dt03.Rows.Count; k++)
        {
            dt03.Rows[k]["CourseName"] = "--";
            if (dt03.Rows[k]["Broswer"].ToString() == "True")
                dt03.Rows[k]["strBroswer"] = "是";
            else
                dt03.Rows[k]["strBroswer"] = "否";
            if (dt03.Rows[k]["SetTop"].ToString() == "True")
                dt03.Rows[k]["strSetTop"] = "是";
            else
                dt03.Rows[k]["strSetTop"] = "否";
            if (dt03.Rows[k]["SetHighLight"].ToString() == "True")
                dt03.Rows[k]["strSetHighLight"] = "是";
            else
                dt03.Rows[k]["strSetHighLight"] = "否";

            dt.Rows.Add(dt03.Rows[k]["Course_Id"], dt03.Rows[k]["CourseName"], dt03.Rows[k]["ClassTasks_Id"], dt03.Rows[k]["ClassID"], dt03.Rows[k]["TasksTitle"], dt03.Rows[k]["EndDate"], dt03.Rows[k]["Broswer"], dt03.Rows[k]["CreatDate"], dt03.Rows[k]["SetTop"], dt03.Rows[k]["SetHighLight"], dt03.Rows[k]["rowstat"], dt03.Rows[k]["strBroswer"], dt03.Rows[k]["strSetTop"], dt03.Rows[k]["strSetHighLight"]);


        }


        rptNew.DataSource = dt;
        rptNew.DataBind();
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            LinkButton lnkbtn_del = (LinkButton)e.Item.FindControl("lnkbtn_del");
            LinkButton lnkbtn_browsr = (LinkButton)e.Item.FindControl("lnkbtn_browsr");
            LinkButton lnkbtn_top = (LinkButton)e.Item.FindControl("lnkbtn_top");
            LinkButton lnkbtn_highlith = (LinkButton)e.Item.FindControl("lnkbtn_highlith");
            Label lab_ID = (Label)e.Item.FindControl("lab_ID");
            lnkbtn_del.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");

            if (lab_ID.Text == "0")
            {
                lnkbtn_editor.Enabled = false;
                lnkbtn_del.Enabled = false;
                lnkbtn_browsr.Enabled = false;
                lnkbtn_top.Enabled = false;
                lnkbtn_highlith.Enabled = false;
            }
        }
    }

    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        string url = "AddClassTask.aspx?CID=" + ViewState["CID"].ToString();
        Script.RedirectURL(url);

        //if (!string.IsNullOrEmpty(txtNewTitle.Text))
        //{
        //    VODLProvince vo = new VODLProvince();
        //    vo.Province = txtNewTitle.Text.Trim();
        //    vo.CityNum = 0;
        //    obj.Add(vo);

        //}
        //txtNewTitle.Text = "";
        //string search = GetSearch();
        //BinderPager(search);
        //BinderPG(search, AspNetPager1.CurrentPageIndex);

    }


  
    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        VOTOClassTasks vo = new VOTOClassTasks();
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");

        if (e.CommandName.ToLower() == "ldel")
        {
            if (objClassTask.Del(int.Parse(e.CommandArgument.ToString())))
                {
                    BinderPG(voClass);
                }
                else
                 {
                    lbl_msg.Text = GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page);
                }
           
        }
        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "AddClassTask.aspx?CTID=" + e.CommandArgument.ToString() + "&CID=" + ViewState["CID"].ToString();
            Script.RedirectURL(url);

        }
        if (e.CommandName.ToLower() == "lsetbrowse")
        {
            vo = objClassTask.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.Broswer == true)
                vo.Broswer = false;
            else
                vo.Broswer = true;
            objClassTask.Update(vo);
            BinderPG(voClass);

        }
        if (e.CommandName.ToLower() == "lsettop")
        {
            vo = objClassTask.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.SetTop == true)
                vo.SetTop = false;
            else
                vo.SetTop = true;
            objClassTask.Update(vo);
            BinderPG(voClass);


        }
        if (e.CommandName.ToLower() == "lsethightlight")
        {
            vo = objClassTask.Load(int.Parse(e.CommandArgument.ToString()));
            if (vo.SetHighLight == true)
                vo.SetHighLight = false;
            else
                vo.SetHighLight = true;
            objClassTask.Update(vo);
            BinderPG(voClass);

        }
        if (e.CommandName.ToLower() == "ladd")
        {
            string url = "AddClassTask.aspx?CourseID=" + e.CommandArgument.ToString() + "&CID=" + ViewState["CID"].ToString();
            Script.RedirectURL(url);

        }
       
    }
    //protected void chkAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    chkCancel.Checked = false;
    //    //全选 / 取消
    //    for (int i = 0; i < rptNew.Items.Count; i++)
    //    {
    //        CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
    //        if (chkAll.Checked)
    //            cbox.Checked = true;
    //        else
    //            cbox.Checked = false;
    //    }
    //}
    //protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    //{
    //    chkAll.Checked = false;
    //    //反选
    //    for (int i = 0; i < rptNew.Items.Count; i++)
    //    {
    //        CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
    //        if (cbox.Checked)
    //            cbox.Checked = false;
    //        else
    //            cbox.Checked = true;
    //    }
    //}
    //protected void btn_DelCheck_Click(object sender, EventArgs e)
    //{
    //    try
    //    {
    //        for (int i = 0; i < rptNew.Items.Count; i++)
    //        {
    //            CheckBox cbox = (CheckBox)rptNew.Items[i].FindControl("Chbox_Check");
    //            Label lab_NewID = (Label)rptNew.Items[i].FindControl("lab_NewID");
    //            if (cbox.Checked)
    //            {

    //                if (objClassTask.Del(int.Parse(lab_NewID.Text)))
    //                    {
                            
    //                    }
    //                    else
    //                    {
    //                        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
    //                    }
                    
    //            }
    //        }
    //        BinderPG(voClass);
    //        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkOK", PowerCreator.LMS.Language.LanagetItemType.Page));
    //    }
    //    catch
    //    {
    //        Script.Alert(this, "msg", GetLanageValueByKey("Msg_ManagerQuestion_WorkNo", PowerCreator.LMS.Language.LanagetItemType.Page));
    //    }
    //}
 
}